package com.gold.consumer.ribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    /** 引用断路器命令 */
    @HystrixCommand(fallbackMethod = "sayHellError")
    public String sayHello(String name){
        /**
         * http://GOLD-PROVIDER-DEMO/hello ，会被解析为 http://ip:port/hello
         * 因本demo均有设置context-path，因此此处需设置为  http://GOLD-PROVIDER-DEMO/gold-provider-demo/hello -> http://localhost:9001/gold-provider-demo/hello
         */
        String url="http://GOLD-PROVIDER-DEMO/gold-provider-demo/hello";
        return restTemplate.getForObject(url+"?name="+name,String.class);
    }

    public String sayHellError(String name){
        return "hello,"+name+", sorry hystrix error";
    }

}
